31. Solution: Add a Touch Selector

Add a Touch Selector Solution

After completing this exercise, you should have touch selectors for each list item in the main layout.

Notes on Solution Code

Today List Item Selector

Remember that touch selectors are drawable resources; the code for the today list item selector that turns colorPrimaryDark when the today list item is selected, it's default color is specified at the end of this file as colorPrimary.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/colorPrimaryDark" android:state_pressed="true" />

  <item android:drawable="@color/colorPrimaryDark" android:state_activated="true" />

  <item android:drawable="@color/colorPrimaryDark" android:state_selected="true" />

  <item android:drawable="@color/colorPrimary" />
</selector>

The general list item selector should look similar but with a default background color when an item is not selected.

Then, to apply a newly created touch selector to a list item, you must set that resource as a background attribute in the layout xml, like so:

android:background="@drawable/today_touch_selector"
android:background="@drawable/touch_selector"

Solution Code

Solution: [S12.03-Solution-TouchSelectors][Diff]